import { nextTick } from "vue";

export function TowDecimal(num: string, length: number) {
  for (var len = (num + "").length; len < length; len = num.length) {
    num = "0" + num;
  }
  return num;
}

export function ZeroFill(num: string, length: number) {
  for (var len = (num + "").length; len < length; len = num.length) {
    num = "0" + num;
  }
  return num;
}

/* 防抖 */
export const debounces = (fn, delay = 300) => {
  let timer = null;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  };
};

/* 节流 */
export const throttle = (fn, wait = 300) => {
  let timer = null;
  return function (...args) {
    let context = this;
    // let args = arguments;
    if (!timer) {
      timer = setTimeout(function () {
        fn.apply(context, args);
        timer = null;
      }, wait);
    }
  };
};

//转义后端字符
export function htmlDecode(str: string) {
  const map = {
    "&amp;": "&",
    "&lt;": "<",
    "&gt;": ">",
    "&quot;": '"',
    "&#39;": "'"
  };
  return !str ? "" : str.replace(/&(amp|lt|gt|quot|#39);/g, m => map[m]);
}

/**
 * @function 截取日期
 * @method SpliceDate(str)
 * @return {string} str 时间 如：2023-11-23 00:00:00
 * @example  SpliceDate("2023-11-23 00:00:00")   // 2023-11-23
 */

export function SpliceDate(str: string) {
  const date = new Date(str);
  const year = date.getFullYear();
  const month = date.getMonth() + 1; // 注意月份是从 0 开始的，所以要加 1
  const day = date.getDate();
  const formattedDate = `${year}-${month < 10 ? "0" + month : month}-${day < 10 ? "0" + day : day
    }`;
  return formattedDate;
}



export function SetTaskListName() {
  let SetTaskName = "";
  const modelId = JSON.parse(sessionStorage.getItem("moduleId"))
  console.log("1", modelId, modelId == 'B')
  switch (modelId) {
    case 'B':
      SetTaskName = "征信大数据";
      break;
    case 'C':
      SetTaskName = "银行大数据";
      break;
    case 'D':
      SetTaskName = "证券大数据";
      break;
    case 'E':
      SetTaskName = "保险大数据";
      break;
    case 'F':
      SetTaskName = "大数据金融监管";
      break;
    case 'G':
      SetTaskName = "大数据供应链金融";
      break;
    default:
      SetTaskName = ""
      break;
  }

  return SetTaskName;
}
